<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2014/10/9
 * Time: 11:12
 * 网点问卷---答案
 */
class NetworkQuestionAnswer extends CActiveRecord
{
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }

    public function tableName()
    {
        return 'network_question_answer';
    }
    /**
     * 得到网点问卷的回答
     * @param $nq_id 网点问卷ID
     */
    public function getByNq($nq_id){
        $data = $this->findAll('nq_id='.$nq_id);
        if ($data) {
            $result = array();
            foreach ($data as $key => $val) {
                $subject_id = $val['subject_id'];
                $result[$subject_id] = $val;
            }
            return $result;
        }
        return $data;
    }
    /**
     * 某个问题的回答
     */
    public function getBySubject($nq_id, $subject_id){
        $data = $this->find('nq_id='.$nq_id.' and subject_id='.$subject_id);
        $info = array();
        if ($data) {
            $info = $data->attributes;
            $info['option'] = explode(',', $info['answer_option']);
        }
        return $info;
    }
	/**
	 * 添加问卷答案
	 * @param type $datas
	 * @return boolean
	 */
	public function addAnswer($datas) {
		$status = true;
		if (!$datas) {
			return false;
		}
		
		foreach ($datas as $item) {
			$answerModel = new NetworkQuestionAnswer();
			
			//添加之前先删除
			$answerModel->deleteAll('nq_id=:nq_id and subject_id=:subject_id', array(
				':nq_id' => $item['nq_id'],
				':subject_id' => $item['subject_id'],
			));
			
			foreach ($item as $field => $value) {
				$answerModel->$field = $value;
			}
			$saveSta = $answerModel->save();
			if ($saveSta) {
				$answerId = $answerModel->attributes['answer_id'];
			}
			$status = $status && $answerId;
		}
		return $status;
	}
}